﻿using Biotech.Log;
using project.B12.application.Ioc;
using project.B12.Model.Common;
using project.B12.Model.Enums;
using project.B12.Model.Models;
using project.B12.ViewModels;
using software.Common.MVVM;
using Software.Common.Util.AppConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace project.B12.application.ViewModels.Setting
{
    public class LanguageSettingViewModel : ObservableObject
    {
		private Dictionary<string, LanguageEnum> languageLists=new Dictionary<string, LanguageEnum>();
		public Dictionary<string, LanguageEnum> LanguageLists
        {
			get { return languageLists; }
			set { languageLists = value; this.RaisePropertyChanged(); }
		}

		private LanguageEnum currentLanguage;
		public LanguageEnum CurrentLanguage
        {
			get { return currentLanguage; }
			set { currentLanguage = value; this.RaisePropertyChanged(); }
		}

        private PublicResourceViewModel _publicResourceViewModel;

        public RelayCommand SaveLanguageCommand => new RelayCommand(SaveLanguage);

        public LanguageSettingViewModel()
        {
            LanguageLists = EnumUtil.GetEnumDisplay<LanguageEnum>(typeof(LanguageEnum));
            CurrentLanguage = GetLanguage();
            ViewModelLocator.PublicResourceViewModel.Language = CurrentLanguage;
            _publicResourceViewModel = ViewModelLocator.PublicResourceViewModel;
        }

        /// <summary>
        /// 设置语言
        /// </summary>
        private void SaveLanguage()
        {
            ChangeLanguage(CurrentLanguage);
            LoggerFactory.Instance.CurrentLogger.LogInfo("切换语言", "", "软件");
            WPF.Controls.Message.Push("语言设置成功，重启后生效！", System.Windows.MessageBoxImage.Information);
        }


        public void ChangeLanguage(LanguageEnum language)
        {
            switch (language)
            {
                case LanguageEnum.Ch:
                    UtilAppConfig.UpdateAppConfig("Language", "zh-CN");
                    break;

                case LanguageEnum.En:
                    UtilAppConfig.UpdateAppConfig("Language", "en");
                    break;
            }

            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(UtilAppConfig.GetItem("Language"));
        }

        public LanguageEnum GetLanguage()
        {
            string? lan;
            if (UtilAppConfig.ExistItem("Language"))
                lan = UtilAppConfig.GetItem("Language");
            else
            {
                lan = "en";
                UtilAppConfig.AddItem("Language", lan);
            }
            switch (lan)
            {
                case "zh-CN":
                    return LanguageEnum.Ch;

                case "en":
                    return LanguageEnum.En;
            }
            return LanguageEnum.En;
        }
    }
}
